import { _decorator, Component, Node ,director, log, AudioClip, Label, find, sys, CCObjectFlags, Prefab, instantiate, Color, resources, game, PhysicsSystem2D, EPhysics2DDrawFlags, ProgressBar } from 'cc';
import { AudioManager } from './tool/AudioManager';
import { PlayerManager } from './PlayerManager';
import { GlobalEvent } from './tool/GlobalEventManager';
import { enemy } from './enemy';



const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {
    // 静态实例，确保全局唯一
    private static _instance: GameManager = null;
    // 游戏是否结束标志，0是表示未开始 ,1 表示进行中，2 表示结束
    private _gameStatus: number = 0;
    // 难度
    private _difficulty: number = 1;






    //杀消灭的数量
    killNum: number = 0;

    //英雄血量
    hp: number = 20;




    
    // 单例访问器
    public static get instance(): GameManager {
        return this._instance;
    }
    //是否完成事件注册
    private hasRegistered: boolean = false; 
    
    onLoad() {
        if (GameManager._instance === null) {
            GameManager._instance = this;
            //持久化这个节点
            director.addPersistRootNode(this.node);
        
            //设置事件监听
            this.registerEventListeners();
        } else {
            // 如果已存在实例，销毁当前节点
            this.node.destroy();
        }
        

    }

    getGameStatus(){return this._gameStatus;}
    getDifficulty(){return this._difficulty;}

     // 专门的事件注册方法
    registerEventListeners() {
        if (this.hasRegistered) { return;}
        log("GameManager注册事件监听");
        // 下面监听所有事件
        GlobalEvent.on('dieEnemy', this.dieEnemy, this);
        GlobalEvent.on('hitPlayer', this.hitPlayer, this);
    }

    dieEnemy(data: any) {
        this.killNum++;
        let node=find('Canvas/player/dir');
        node.getComponent(Label).string="还有 "+(15-this.killNum)+" 个敌人，待消灭！" ;
        if(this.killNum>=15){
            this.initEnd('shengli');
        }
    }
    hitPlayer(data: any) {
         this.hp--;
         let node=find('Canvas/player/playBar/ProgressBar');
         node.getComponent(ProgressBar).progress=this.hp/20;
         if(this.hp<=0){
            this.initEnd('die');
        }
    }

    start() {
        console.log("GameManager初始化完成");
        this.initGame();
        AudioManager.instance.playBGM('sound/bg');
    }
    // 退出游戏
    public exitGame() {
        log("exitGame");
        if (sys.isNative) {
            // 退出游戏
            game.end();
            // director.end();
        } else {
            // 退出游戏
            window.history.back();
        }
    }
    /**
     * 初始化游戏
     */
    public initGame() {
        this._gameStatus = 0;
        this.killNum = 0;
        this.hp = 20;
        let node=find('Canvas/player/playBar/ProgressBar');
        node.getComponent(ProgressBar).progress=1;

        let dir=find('Canvas/player/dir');
        dir.getComponent(Label).string="还有 "+(15-this.killNum)+" 个敌人，待消灭！" ;


        let start=find('Canvas/start');
        start.active=true;
        let end=find('Canvas/end');
        end.active=false;
        let player=find('Canvas/player');
        player.setPosition(0,0,0);
        let playerBody=player.getChildByName('body');
        playerBody.setPosition(0,-40,0);
        let enemy=find('Canvas/player/enemy');
        enemy.removeAllChildren();
    }
    /**
     * 开始游戏
     */
    public paying(event: Event, customEventData: string) {
        this._difficulty = parseInt(customEventData);
        this._gameStatus = 1;
        let start=find('Canvas/start');
        start.active=false;
        let end=find('Canvas/end');
        end.active=false;



        let enemyNode=find('Canvas/player/enemy');
        enemyNode.getComponent(enemy).shengcheng();

    }
    /**
     * 结束游戏
     */
    public initEnd(str: string) {
        this._gameStatus = 3;
        if (str == 'die') {
            AudioManager.instance.playSFX('sound/shibai');
        } else {
            AudioManager.instance.playSFX('sound/shengli');
        }

       //  AudioManager.instance.playSFX('sound/shengli');

       let start=find('Canvas/start');
        start.active=false;
        let end=find('Canvas/end');
        end.getChildByName('Label').getComponent(Label).string= str=='die'?'你被敌人打死了！':'恭喜你，你赢了！';
        end.active=true;

        let player=find('Canvas/player');


        end.setPosition(player.position.x,player.position.y,0);
    }
    protected onDestroy(): void {
        GlobalEvent.targetOff(this);
    }
}


